package edu.ccut.saturn.studio.authority.model;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;

public class AuthorityModel {

	private static final String RESOURCE_ATTRIBUTE_DESCRIPTION = "description";
	private static final String RESOURCE_ATTRIBUTE_VALUE = "value";
	private static final String RESOURCE_ATTRIBUTE_NAME = "name";
	private static final String RESOURCE_ATTRIBUTE_ID = "id";
	public static final String AUTHORITY_ATTRIBUTE_PROJECTNAME = "projectName";
	private static final String RESOURCES_ELEMENT_RESOURCE = "resource";
	private static final String AUTHORITY_ELEMENT_RESOURCES = "resources";
	private Document document;
	private AuthorityVO authorityVO = null;

	/**
	 * 构造函数
	 * 
	 * @param document
	 */
	public AuthorityModel(Document document) {
		this.document = document;
	}

	public AuthorityVO getAuthorityVO() {

		if(authorityVO == null) {
			List<Resource> resources = this.getResources();
			authorityVO = new AuthorityVO();
			
			authorityVO.setResources(resources);	
			}	
		return authorityVO;
	}
	
	private Node getResourceNode() {
		Node authorityNode = SaturnW3CUtil.getFirstChildByName(this.document
				.getDocumentElement(), AUTHORITY_ELEMENT_RESOURCES);
		return authorityNode;
	}
		
	public Document getDocument() {
		return document;
	}

	
	/**
	 * 获得resources的id值(用于添加resource)
	 */
	public int getResourceId() {
		Node resourcesNode = this.getResourceNode();
		List<Node> resources = SaturnW3CUtil.getChildrenByName(resourcesNode, RESOURCES_ELEMENT_RESOURCE);

		return resources.size() + 1;
	}
	
	/**
	 * 获得resource的List列表值
	 */
	public List<Resource> getResources() {
		Node resourcesNode = this.getResourceNode();
		List<Node> resourceNodes = SaturnW3CUtil.getChildrenByName(resourcesNode, RESOURCES_ELEMENT_RESOURCE);
		List<Resource> resources = new ArrayList<Resource>();
		 
		if (resourceNodes != null) {
			for (Node resourceNode :resourceNodes) {
				Resource resource = new Resource();
				resource.setId(Integer.parseInt(SaturnW3CUtil.getNodeAttributeValue(resourceNode, RESOURCE_ATTRIBUTE_ID)));
				resource.setName(SaturnW3CUtil.getNodeAttributeValue(resourceNode, RESOURCE_ATTRIBUTE_NAME));
				resource.setValue(SaturnW3CUtil.getNodeAttributeValue(resourceNode, RESOURCE_ATTRIBUTE_VALUE));
				resource.setDescritpion(SaturnW3CUtil.getNodeAttributeValue(resourceNode, RESOURCE_ATTRIBUTE_DESCRIPTION));
				resources.add(resource);
			}
		}
		return  resources;
	}
	
	/**
	 * 
	 */
	public void updateResource(List<Resource> resources) {
		Node resourcesNode = this.getResourceNode();
		SaturnW3CUtil.removeChildren(resourcesNode);
		for (Resource resource : resources) {
			Node resourceNode = SaturnW3CUtil.createChildNode(resourcesNode, RESOURCES_ELEMENT_RESOURCE, "");
			SaturnW3CUtil.createAttribute(resourceNode, RESOURCE_ATTRIBUTE_ID,resource.getId() + "");
			SaturnW3CUtil.createAttribute(resourceNode, RESOURCE_ATTRIBUTE_NAME, resource.getName());
			SaturnW3CUtil.createAttribute(resourceNode, RESOURCE_ATTRIBUTE_VALUE, resource.getValue());
			SaturnW3CUtil.createAttribute(resourceNode, RESOURCE_ATTRIBUTE_DESCRIPTION, resource.getDescritpion());
		}
		SaturnW3CUtil.format(resourcesNode);
		
		if (this.authorityVO != null) {
			this.authorityVO.setResources(resources);
		}
	}
	public String getProjectName() {
		Node rootNode = getDocument().getDocumentElement();
		String projectName = SaturnW3CUtil.getNodeAttributeValue(rootNode, AUTHORITY_ATTRIBUTE_PROJECTNAME);
			
		return projectName.trim();
	}
	
	public void updateResourceAttribute(String id, String attributeName, String newValue) {
		Node resourcesNode = this.getResourceNode();
		Node resourceNode = SaturnW3CUtil.getChildByNameAndAttribute(resourcesNode, RESOURCES_ELEMENT_RESOURCE, RESOURCE_ATTRIBUTE_ID, id);
		SaturnW3CUtil.createAttribute(resourceNode, attributeName, newValue);
		
		SaturnW3CUtil.format(resourcesNode);
		
		if (this.authorityVO != null) {
			List<Resource> resources = this.authorityVO.getResources();
			this.authorityVO.setResources(resources);
		}
	}
	
}